package com.example.javaui.slice.component;

import com.example.javaui.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.DatePicker;
import ohos.agp.components.Picker;
import ohos.agp.components.TimePicker;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

public class PickerSlice extends AbilitySlice {

    private Picker mPicker;
    private TimePicker mTimePicker;
    private DatePicker mDatePicker;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_component_picker);


        mPicker = (Picker) findComponentById(ResourceTable.Id_picker);
        mPicker.setCompoundElementPadding(50);
        mPicker.setSelectorItemNum(3);  // 默认为5
        mPicker.setElementFormatter(new Picker.ElementFormatter() {
            @Override
            public Element leftElement(int i) {
                // 左侧图像元素
                ShapeElement element = new ShapeElement();
                element.setRgbColor(new RgbColor(255, 0,0));
                return element;
            }

            @Override
            public Element rightElement(int i) {
                // 右侧图像元素
                return null;
            }
        });
        // 将索引转换为格式化文本
        mPicker.setFormatter(new Picker.Formatter() {
            @Override
            public String format(int i) {
                return "选项:" + i;
            }
        });
        // mPicker.setDisplayedData(new String[]{"北京市", "天津市", "河北省", "山西省"});

        mDatePicker = (DatePicker) findComponentById(ResourceTable.Id_date_picker);
        mDatePicker.updateDate(2008, 8, 8);
        mDatePicker.setDateOrder(DatePicker.DateOrder.YMD);
        mDatePicker.setSelectorItemNum(4);  // 默认为5
        mDatePicker.setSelectedNormalTextMarginRatio(6);
        mDatePicker.setOperatedTextColor(Color.YELLOW);


        mTimePicker = (TimePicker) findComponentById(ResourceTable.Id_time_picker);
        mTimePicker.set24Hour(false); // 12小时制
        mTimePicker.setAmPmOrder(TimePicker.AmPmOrder.END); // AM/PM选项放置在最后
        mTimePicker.setAmPmStrings("清醒上午", "昏睡下午");
        mTimePicker.setRange(new int[]{8,0,0,21,59,59}); // 可选时间:早8点-晚10点
        // 设置框线
        ShapeElement redElement = new ShapeElement();
        redElement.setRgbColor(new RgbColor(255, 0,0));
        mTimePicker.setDisplayedLinesElements(redElement, redElement);
        // 渐变背景设置为灰色
        mTimePicker.setShaderColor(new Color(Color.rgb(150,150,150)));
    }

    private Element generateElement(int red, int green, int blue) {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(red, green,blue));
        return element;
    }

}
